package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.persistence.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CatalogService {

    //业务逻辑层调用数据访问层
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;


    public List<Category> getCategoryList() {

        return categoryMapper.getCategoryList();
    }

    public Category getCategory(String categoryId) {

        return categoryMapper.getCategory(categoryId);
    }

    public Product getProduct(String productId) {

        return productMapper.getProduct(productId);
    }

    public List<Product> getProductListByCategory(String categoryId) {
        return productMapper.getProductListByCategory(categoryId);
    }

    /* TODO enable using more than one keyword
    *
    * */
    public List<Product> searchProductList(String keyword) {
        return productMapper.searchProductList("%" + keyword.toLowerCase() + "%");
    }

    public List<Item> getItemListByProduct(String productId) {
        return itemMapper.getItemListByProduct(productId);
    }

    public Item getItem(String itemId) {
        return itemMapper.getItem(itemId);
    }

    public boolean isItemInStock(String itemId) {
        return itemMapper.getInventoryQuantity(itemId) > 0;
    }












    //测试 注入
//    @Autowired
//    private CategoryMapper categoryMapper;
//    public Category getCategory(String categoryId){
//           return categoryMapper.getCategory(categoryId);
//    }

    @Autowired
    private AccountMapper accountMapper;
    public Account getAccount(String username){
        return accountMapper.getAccountByUsername(username);
    }
    public Account getAccountbyUsernameandPassword(Account account){
        return accountMapper.getAccountByUsernameAndPassword(account);
    }

//    @Autowired
//    private ProductMapper productMapper;
//    public Product getProduct(String productId){
//        return productMapper.getProduct(productId);
//    }

//    @Autowired
//    private ItemMapper itemMapper;
//    public Item getItem(String itemId){
//        return  itemMapper.getItem(itemId);
//    }
    @Autowired
    private CartMapper cartMapper;
//    public Cart GetCart(String userid){
//        return cartMapper.getCart(userid);
//    }

    @Autowired
    private LineItemMapper lineItemMapper;
    public List<LineItem> testGetLineItem(int orderId){
        return lineItemMapper.getLineItemsByOrderId(orderId);
    }

    @Autowired
    private OrderMapper orderMapper;
    public Order getOrder(int orderId){
        return orderMapper.getOrder(orderId);
    }
}
